using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using CatalogSystemProxy;
using System.Xml;
using System.Data;

namespace CatalogManager.CategoryForms
{
	/// <summary>
	/// Summary description for CatalogEditor.
	/// </summary>
	public class CategoryEditor : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPageCategoryProperties;
		private System.Windows.Forms.TabPage tabPageAssignedCategories;
		private System.Windows.Forms.TabPage tabPageCategoryRelationship;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnSaveNBack;
		private System.Windows.Forms.Button btnBack;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Label lblAssianedCategories;
		private System.Windows.Forms.Label lblParentCategories;
		private System.Windows.Forms.Button btnRemoveParentCategory;
		private System.Windows.Forms.Button btnAddParentCategory;
		private System.Windows.Forms.ListBox listBoxAssiangedCategories;
		private System.Windows.Forms.TreeView treeViewParentCategoreis;
		private System.Windows.Forms.Label lblAssignedCategories;
		private System.Windows.Forms.Label lblAvailableCategories;
		private System.Windows.Forms.Button btnRemoveChildCategory;
		private System.Windows.Forms.Button btnAddChildCategory;
		private System.Windows.Forms.Label lblPrimaryParentCategory;
		private System.Windows.Forms.Label lblCurrentCategory;
		private System.Windows.Forms.TextBox txtParentCategory;
		private System.Windows.Forms.TextBox txtCurrentCategory;
		private System.Windows.Forms.ListBox listBoxAssignedChildCategories;
		private System.Windows.Forms.DataGrid dataGridRelationShip;
		private System.Windows.Forms.Button btnRelationShipCancel;
		private System.Windows.Forms.Button btnRelationShipRemove;
		private System.Windows.Forms.Button btnRelationShipEdit;
		private System.Windows.Forms.Button btnRelationShipNew;

		private CatalogSystem CSService = null;
		public DataSet CategoryProperties = null;
		private System.Windows.Forms.Button btnSet;
		private System.Windows.Forms.Button btnClear;
		private System.Windows.Forms.ListBox listBoxAvailableChildCategories;
		private System.Windows.Forms.ImageList TreeViewimageList;
		private System.ComponentModel.IContainer components;

		public CategoryEditor()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(CategoryEditor));
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPageCategoryProperties = new System.Windows.Forms.TabPage();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.tabPageAssignedCategories = new System.Windows.Forms.TabPage();
			this.listBoxAvailableChildCategories = new System.Windows.Forms.ListBox();
			this.btnClear = new System.Windows.Forms.Button();
			this.btnSet = new System.Windows.Forms.Button();
			this.lblAssignedCategories = new System.Windows.Forms.Label();
			this.lblAvailableCategories = new System.Windows.Forms.Label();
			this.lblAssianedCategories = new System.Windows.Forms.Label();
			this.lblParentCategories = new System.Windows.Forms.Label();
			this.btnRemoveChildCategory = new System.Windows.Forms.Button();
			this.btnAddChildCategory = new System.Windows.Forms.Button();
			this.btnRemoveParentCategory = new System.Windows.Forms.Button();
			this.btnAddParentCategory = new System.Windows.Forms.Button();
			this.lblPrimaryParentCategory = new System.Windows.Forms.Label();
			this.lblCurrentCategory = new System.Windows.Forms.Label();
			this.txtParentCategory = new System.Windows.Forms.TextBox();
			this.txtCurrentCategory = new System.Windows.Forms.TextBox();
			this.listBoxAssignedChildCategories = new System.Windows.Forms.ListBox();
			this.listBoxAssiangedCategories = new System.Windows.Forms.ListBox();
			this.treeViewParentCategoreis = new System.Windows.Forms.TreeView();
			this.TreeViewimageList = new System.Windows.Forms.ImageList(this.components);
			this.tabPageCategoryRelationship = new System.Windows.Forms.TabPage();
			this.dataGridRelationShip = new System.Windows.Forms.DataGrid();
			this.btnRelationShipCancel = new System.Windows.Forms.Button();
			this.btnRelationShipRemove = new System.Windows.Forms.Button();
			this.btnRelationShipEdit = new System.Windows.Forms.Button();
			this.btnRelationShipNew = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnSaveNBack = new System.Windows.Forms.Button();
			this.btnBack = new System.Windows.Forms.Button();
			this.panel1 = new System.Windows.Forms.Panel();
			this.tabControl1.SuspendLayout();
			this.tabPageCategoryProperties.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.tabPageAssignedCategories.SuspendLayout();
			this.tabPageCategoryRelationship.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGridRelationShip)).BeginInit();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					  this.tabPageCategoryProperties,
																					  this.tabPageAssignedCategories,
																					  this.tabPageCategoryRelationship});
			this.tabControl1.Location = new System.Drawing.Point(8, 8);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(560, 432);
			this.tabControl1.TabIndex = 0;
			// 
			// tabPageCategoryProperties
			// 
			this.tabPageCategoryProperties.Controls.AddRange(new System.Windows.Forms.Control[] {
																									this.dataGrid1});
			this.tabPageCategoryProperties.Location = new System.Drawing.Point(4, 22);
			this.tabPageCategoryProperties.Name = "tabPageCategoryProperties";
			this.tabPageCategoryProperties.Size = new System.Drawing.Size(552, 406);
			this.tabPageCategoryProperties.TabIndex = 0;
			this.tabPageCategoryProperties.Text = "Category Properties";
			// 
			// dataGrid1
			// 
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(8, 8);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(536, 392);
			this.dataGrid1.TabIndex = 0;
			// 
			// tabPageAssignedCategories
			// 
			this.tabPageAssignedCategories.Controls.AddRange(new System.Windows.Forms.Control[] {
																									this.listBoxAvailableChildCategories,
																									this.btnClear,
																									this.btnSet,
																									this.lblAssignedCategories,
																									this.lblAvailableCategories,
																									this.lblAssianedCategories,
																									this.lblParentCategories,
																									this.btnRemoveChildCategory,
																									this.btnAddChildCategory,
																									this.btnRemoveParentCategory,
																									this.btnAddParentCategory,
																									this.lblPrimaryParentCategory,
																									this.lblCurrentCategory,
																									this.txtParentCategory,
																									this.txtCurrentCategory,
																									this.listBoxAssignedChildCategories,
																									this.listBoxAssiangedCategories,
																									this.treeViewParentCategoreis});
			this.tabPageAssignedCategories.Location = new System.Drawing.Point(4, 22);
			this.tabPageAssignedCategories.Name = "tabPageAssignedCategories";
			this.tabPageAssignedCategories.Size = new System.Drawing.Size(552, 406);
			this.tabPageAssignedCategories.TabIndex = 1;
			this.tabPageAssignedCategories.Text = "Assigned Categories";
			// 
			// listBoxAvailableChildCategories
			// 
			this.listBoxAvailableChildCategories.Location = new System.Drawing.Point(8, 304);
			this.listBoxAvailableChildCategories.Name = "listBoxAvailableChildCategories";
			this.listBoxAvailableChildCategories.Size = new System.Drawing.Size(216, 95);
			this.listBoxAvailableChildCategories.TabIndex = 18;
			this.listBoxAvailableChildCategories.SelectedIndexChanged += new System.EventHandler(this.listBoxAvailableChildCategories_SelectedIndexChanged);
			// 
			// btnClear
			// 
			this.btnClear.Enabled = false;
			this.btnClear.Location = new System.Drawing.Point(400, 248);
			this.btnClear.Name = "btnClear";
			this.btnClear.TabIndex = 17;
			this.btnClear.Text = "Clear";
			this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
			// 
			// btnSet
			// 
			this.btnSet.Enabled = false;
			this.btnSet.Location = new System.Drawing.Point(320, 248);
			this.btnSet.Name = "btnSet";
			this.btnSet.TabIndex = 16;
			this.btnSet.Text = "Set";
			this.btnSet.Click += new System.EventHandler(this.btnSet_Click);
			// 
			// lblAssignedCategories
			// 
			this.lblAssignedCategories.Location = new System.Drawing.Point(320, 280);
			this.lblAssignedCategories.Name = "lblAssignedCategories";
			this.lblAssignedCategories.Size = new System.Drawing.Size(224, 16);
			this.lblAssignedCategories.TabIndex = 15;
			this.lblAssignedCategories.Text = "Assigned child categories:";
			// 
			// lblAvailableCategories
			// 
			this.lblAvailableCategories.Location = new System.Drawing.Point(8, 280);
			this.lblAvailableCategories.Name = "lblAvailableCategories";
			this.lblAvailableCategories.Size = new System.Drawing.Size(216, 16);
			this.lblAvailableCategories.TabIndex = 14;
			this.lblAvailableCategories.Text = "Available child categories:";
			// 
			// lblAssianedCategories
			// 
			this.lblAssianedCategories.Location = new System.Drawing.Point(320, 16);
			this.lblAssianedCategories.Name = "lblAssianedCategories";
			this.lblAssianedCategories.Size = new System.Drawing.Size(216, 16);
			this.lblAssianedCategories.TabIndex = 13;
			this.lblAssianedCategories.Text = "Assianged Parent Categories:";
			// 
			// lblParentCategories
			// 
			this.lblParentCategories.Location = new System.Drawing.Point(16, 16);
			this.lblParentCategories.Name = "lblParentCategories";
			this.lblParentCategories.Size = new System.Drawing.Size(192, 16);
			this.lblParentCategories.TabIndex = 12;
			this.lblParentCategories.Text = "Available Parent Categories:";
			// 
			// btnRemoveChildCategory
			// 
			this.btnRemoveChildCategory.Enabled = false;
			this.btnRemoveChildCategory.Location = new System.Drawing.Point(232, 344);
			this.btnRemoveChildCategory.Name = "btnRemoveChildCategory";
			this.btnRemoveChildCategory.TabIndex = 11;
			this.btnRemoveChildCategory.Text = "< Remove";
			this.btnRemoveChildCategory.Click += new System.EventHandler(this.btnRemoveChildCategory_Click);
			// 
			// btnAddChildCategory
			// 
			this.btnAddChildCategory.Enabled = false;
			this.btnAddChildCategory.Location = new System.Drawing.Point(232, 312);
			this.btnAddChildCategory.Name = "btnAddChildCategory";
			this.btnAddChildCategory.TabIndex = 10;
			this.btnAddChildCategory.Text = "Add >";
			this.btnAddChildCategory.Click += new System.EventHandler(this.btnAddChildCategory_Click);
			// 
			// btnRemoveParentCategory
			// 
			this.btnRemoveParentCategory.Enabled = false;
			this.btnRemoveParentCategory.Location = new System.Drawing.Point(232, 88);
			this.btnRemoveParentCategory.Name = "btnRemoveParentCategory";
			this.btnRemoveParentCategory.TabIndex = 9;
			this.btnRemoveParentCategory.Text = "< Remove";
			this.btnRemoveParentCategory.Click += new System.EventHandler(this.btnRemoveParentCategory_Click);
			// 
			// btnAddParentCategory
			// 
			this.btnAddParentCategory.Enabled = false;
			this.btnAddParentCategory.Location = new System.Drawing.Point(232, 48);
			this.btnAddParentCategory.Name = "btnAddParentCategory";
			this.btnAddParentCategory.TabIndex = 8;
			this.btnAddParentCategory.Text = "Add >";
			this.btnAddParentCategory.Click += new System.EventHandler(this.btnAddParentCategory_Click);
			// 
			// lblPrimaryParentCategory
			// 
			this.lblPrimaryParentCategory.Location = new System.Drawing.Point(320, 192);
			this.lblPrimaryParentCategory.Name = "lblPrimaryParentCategory";
			this.lblPrimaryParentCategory.Size = new System.Drawing.Size(224, 16);
			this.lblPrimaryParentCategory.TabIndex = 7;
			this.lblPrimaryParentCategory.Text = "Primary Parent Category";
			// 
			// lblCurrentCategory
			// 
			this.lblCurrentCategory.Location = new System.Drawing.Point(8, 192);
			this.lblCurrentCategory.Name = "lblCurrentCategory";
			this.lblCurrentCategory.Size = new System.Drawing.Size(216, 16);
			this.lblCurrentCategory.TabIndex = 6;
			this.lblCurrentCategory.Text = "Current Category";
			// 
			// txtParentCategory
			// 
			this.txtParentCategory.Enabled = false;
			this.txtParentCategory.Location = new System.Drawing.Point(320, 216);
			this.txtParentCategory.Name = "txtParentCategory";
			this.txtParentCategory.Size = new System.Drawing.Size(224, 20);
			this.txtParentCategory.TabIndex = 5;
			this.txtParentCategory.Text = "";
			// 
			// txtCurrentCategory
			// 
			this.txtCurrentCategory.Enabled = false;
			this.txtCurrentCategory.Location = new System.Drawing.Point(8, 216);
			this.txtCurrentCategory.Name = "txtCurrentCategory";
			this.txtCurrentCategory.Size = new System.Drawing.Size(216, 20);
			this.txtCurrentCategory.TabIndex = 4;
			this.txtCurrentCategory.Text = "";
			// 
			// listBoxAssignedChildCategories
			// 
			this.listBoxAssignedChildCategories.Location = new System.Drawing.Point(320, 304);
			this.listBoxAssignedChildCategories.Name = "listBoxAssignedChildCategories";
			this.listBoxAssignedChildCategories.Size = new System.Drawing.Size(224, 95);
			this.listBoxAssignedChildCategories.TabIndex = 3;
			this.listBoxAssignedChildCategories.SelectedIndexChanged += new System.EventHandler(this.listBoxAssignedChildCategories_SelectedIndexChanged);
			// 
			// listBoxAssiangedCategories
			// 
			this.listBoxAssiangedCategories.Location = new System.Drawing.Point(320, 40);
			this.listBoxAssiangedCategories.Name = "listBoxAssiangedCategories";
			this.listBoxAssiangedCategories.Size = new System.Drawing.Size(224, 147);
			this.listBoxAssiangedCategories.TabIndex = 2;
			this.listBoxAssiangedCategories.SelectedIndexChanged += new System.EventHandler(this.listBoxAssiangedCategories_SelectedIndexChanged);
			// 
			// treeViewParentCategoreis
			// 
			this.treeViewParentCategoreis.ImageList = this.TreeViewimageList;
			this.treeViewParentCategoreis.Location = new System.Drawing.Point(8, 40);
			this.treeViewParentCategoreis.Name = "treeViewParentCategoreis";
			this.treeViewParentCategoreis.Size = new System.Drawing.Size(216, 144);
			this.treeViewParentCategoreis.TabIndex = 0;
			this.treeViewParentCategoreis.Click += new System.EventHandler(this.treeViewParentCategoreis_Click);
			this.treeViewParentCategoreis.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeViewParentCategoreis_AfterSelect);
			// 
			// TreeViewimageList
			// 
			this.TreeViewimageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth24Bit;
			this.TreeViewimageList.ImageSize = new System.Drawing.Size(16, 16);
			this.TreeViewimageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("TreeViewimageList.ImageStream")));
			this.TreeViewimageList.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// tabPageCategoryRelationship
			// 
			this.tabPageCategoryRelationship.Controls.AddRange(new System.Windows.Forms.Control[] {
																									  this.dataGridRelationShip,
																									  this.btnRelationShipCancel,
																									  this.btnRelationShipRemove,
																									  this.btnRelationShipEdit,
																									  this.btnRelationShipNew});
			this.tabPageCategoryRelationship.Location = new System.Drawing.Point(4, 22);
			this.tabPageCategoryRelationship.Name = "tabPageCategoryRelationship";
			this.tabPageCategoryRelationship.Size = new System.Drawing.Size(552, 406);
			this.tabPageCategoryRelationship.TabIndex = 2;
			this.tabPageCategoryRelationship.Text = "Category Relationship";
			// 
			// dataGridRelationShip
			// 
			this.dataGridRelationShip.DataMember = "";
			this.dataGridRelationShip.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGridRelationShip.Location = new System.Drawing.Point(8, 8);
			this.dataGridRelationShip.Name = "dataGridRelationShip";
			this.dataGridRelationShip.Size = new System.Drawing.Size(536, 360);
			this.dataGridRelationShip.TabIndex = 12;
			// 
			// btnRelationShipCancel
			// 
			this.btnRelationShipCancel.Location = new System.Drawing.Point(360, 376);
			this.btnRelationShipCancel.Name = "btnRelationShipCancel";
			this.btnRelationShipCancel.TabIndex = 11;
			this.btnRelationShipCancel.Text = "Cancel";
			this.btnRelationShipCancel.Click += new System.EventHandler(this.btnRelationShipCancel_Click);
			// 
			// btnRelationShipRemove
			// 
			this.btnRelationShipRemove.Location = new System.Drawing.Point(272, 376);
			this.btnRelationShipRemove.Name = "btnRelationShipRemove";
			this.btnRelationShipRemove.TabIndex = 9;
			this.btnRelationShipRemove.Text = "Remove";
			this.btnRelationShipRemove.Click += new System.EventHandler(this.btnRelationShipRemove_Click);
			// 
			// btnRelationShipEdit
			// 
			this.btnRelationShipEdit.Location = new System.Drawing.Point(184, 376);
			this.btnRelationShipEdit.Name = "btnRelationShipEdit";
			this.btnRelationShipEdit.TabIndex = 8;
			this.btnRelationShipEdit.Text = "Edit";
			this.btnRelationShipEdit.Click += new System.EventHandler(this.btnRelationShipEdit_Click);
			// 
			// btnRelationShipNew
			// 
			this.btnRelationShipNew.Location = new System.Drawing.Point(96, 376);
			this.btnRelationShipNew.Name = "btnRelationShipNew";
			this.btnRelationShipNew.TabIndex = 7;
			this.btnRelationShipNew.Text = "New";
			this.btnRelationShipNew.Click += new System.EventHandler(this.btnRelationShipNew_Click);
			// 
			// btnSave
			// 
			this.btnSave.Location = new System.Drawing.Point(344, 8);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(96, 23);
			this.btnSave.TabIndex = 6;
			this.btnSave.Text = "Save";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnSaveNBack
			// 
			this.btnSaveNBack.Location = new System.Drawing.Point(240, 8);
			this.btnSaveNBack.Name = "btnSaveNBack";
			this.btnSaveNBack.Size = new System.Drawing.Size(96, 23);
			this.btnSaveNBack.TabIndex = 5;
			this.btnSaveNBack.Text = "Save and Back";
			this.btnSaveNBack.Click += new System.EventHandler(this.btnSaveNBack_Click);
			// 
			// btnBack
			// 
			this.btnBack.Location = new System.Drawing.Point(136, 8);
			this.btnBack.Name = "btnBack";
			this.btnBack.Size = new System.Drawing.Size(96, 23);
			this.btnBack.TabIndex = 4;
			this.btnBack.Text = "Back";
			this.btnBack.Click += new System.EventHandler(this.btnBack_Click);
			// 
			// panel1
			// 
			this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
																				 this.btnSave,
																				 this.btnSaveNBack,
																				 this.btnBack});
			this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel1.Location = new System.Drawing.Point(0, 444);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(574, 40);
			this.panel1.TabIndex = 8;
			// 
			// CategoryEditor
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(574, 484);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.panel1,
																		  this.tabControl1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "CategoryEditor";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Category Editor";
			this.Load += new System.EventHandler(this.CategoryEditor_Load);
			this.tabControl1.ResumeLayout(false);
			this.tabPageCategoryProperties.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.tabPageAssignedCategories.ResumeLayout(false);
			this.tabPageCategoryRelationship.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGridRelationShip)).EndInit();
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private DataSet CreateVerticalDataSet()
		{
			DataSet ds = new DataSet();
			DataTable dt = new DataTable();
			ds.Tables.Add(dt);
			// create two columns DisplayName and Value
			ds.Tables[0].Columns.Add("DisplayName", typeof(string));
			ds.Tables[0].Columns.Add("Value", typeof(string));

			return ds;
		}

		private DataSet MergeVerticaleDataSetToProdcutDataSet(DataTable CategoryDataSet)
		{
			// create a vertical dataset
			DataSet ds = CreateVerticalDataSet();
			// get the product definition so we can show only what is assigned to the product
			DataTable dtDefinition = CategoryProperties.Tables["DefinitionProperties"];

			// create name row
			DataRow rowName = ds.Tables[0].NewRow();
			rowName["DisplayName"] = "CategoryName";
			rowName["Value"] = CategoryDataSet.Rows[0]["categoryname"].ToString();
			ds.Tables[0].Rows.Add(rowName);

			// create display name row
			DataRow rowDispalyName = ds.Tables[0].NewRow();
			rowDispalyName["DisplayName"] = "DisplayName";
			rowDispalyName["Value"] = CategoryDataSet.Rows[0]["displayname"].ToString();
			ds.Tables[0].Rows.Add(rowDispalyName);

			// remove the columns we don't want
			foreach(DataRow definiotnRow in dtDefinition.Rows)
			{
				int idx = CategoryDataSet.Columns.IndexOf(definiotnRow["PropertyName"].ToString());
				if(idx > 0 && definiotnRow["PropertyType"].ToString()=="0")
				{
					// start binding
					DataRow row = ds.Tables[0].NewRow();
					row["DisplayName"] = definiotnRow["PropertyName"].ToString();
					row["Value"] = CategoryDataSet.Rows[0][definiotnRow["PropertyName"].ToString()];
					ds.Tables[0].Rows.Add(row);
				}
			}

			// create price row
			DataRow rowPrice = ds.Tables[0].NewRow();
			rowPrice["DisplayName"] = "Price";
			rowPrice["Value"] = CategoryDataSet.Rows[0]["cy_list_price"].ToString();
			ds.Tables[0].Rows.Add(rowPrice);

			return ds;
		}

		private void CategoryEditor_Load(object sender, System.EventArgs e)
		{
			// read the xml file and get the file path
			XmlDocument doc = new XmlDocument();
			doc.Load("SiteConfig.xml");
			CSService = new CatalogSystem(doc.FirstChild.InnerText);

			if(this.CategoryProperties!=null)
			{
				foreach(DataTable dt in CategoryProperties.Tables)
				{
					switch(dt.TableName)
					{
						case "CategoryProperties":
							// load the custom properties
							// do some magic here disable the Display name so we can't change the properties
							DataGridTableStyle dgStyle = new DataGridTableStyle();
							dgStyle.AllowSorting = false;
							dgStyle.MappingName = "Table1";
							DataGridTextBoxColumn DisplayName = new DataGridTextBoxColumn();
							DisplayName.MappingName = "DisplayName";
							DisplayName.HeaderText = "DisplayName";
							DisplayName.Width = 100;
							DisplayName.ReadOnly = true;
							dgStyle.GridColumnStyles.Add(DisplayName);
							// Add a second column style.
							DataGridColumnStyle Value = new DataGridTextBoxColumn();
							Value.MappingName = "Value";
							Value.HeaderText = "Value";
							Value.Width = 150;
							Value.ReadOnly = false;
							dgStyle.GridColumnStyles.Add(Value);
							dataGrid1.TableStyles.Add(dgStyle);
							// disallow new rows from being created
							dataGrid1.DataSource = MergeVerticaleDataSetToProdcutDataSet(dt).Tables[0];
							CurrencyManager cm = (CurrencyManager)this.BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
							((DataView)cm.List).AllowNew = false;
							break;
						case "ChildCategories":
							// add the current child categories
							foreach(DataRow drChildCategory in dt.Rows)
							{
								listBoxAssignedChildCategories.Items.Add(drChildCategory["CategoryName"]);
							}
							break;
						case "ParentCategories":
							// add the current child categories
							foreach(DataRow drParentCategory in dt.Rows)
							{
								listBoxAssiangedCategories.Items.Add(drParentCategory["Name"]);
							}
							break;
						case "AvailableChilds":
							foreach(DataRow dr in dt.Rows)
							{
								listBoxAvailableChildCategories.Items.Add(dr["CategoryName"]);
							}
							break;
						case "RelatedProducts":
							// do some magic here disable the Display name so we can't change the properties
							DataGridTableStyle dgStyleRelatedProducts = new DataGridTableStyle();
							dgStyleRelatedProducts.AllowSorting = false;
							dgStyleRelatedProducts.MappingName = "RelatedProducts";
							DataGridTextBoxColumn TargetCatalog = new DataGridTextBoxColumn();
							TargetCatalog.MappingName = "TargetCatalog";
							TargetCatalog.HeaderText = "TargetCatalog";
							TargetCatalog.Width = 100;
							TargetCatalog.ReadOnly = true;
							dgStyleRelatedProducts.GridColumnStyles.Add(TargetCatalog);
							// Add a second column style.
							DataGridColumnStyle ProductID = new DataGridTextBoxColumn();
							ProductID.MappingName = "ProductID";
							ProductID.HeaderText = "ProductID";
							ProductID.Width = 100;
							ProductID.ReadOnly = true;
							dgStyleRelatedProducts.GridColumnStyles.Add(ProductID);
							// Add a second column style.
							DataGridColumnStyle RelationShipName = new DataGridTextBoxColumn();
							RelationShipName.MappingName = "RelationShipName";
							RelationShipName.HeaderText = "RelationShipName";
							RelationShipName.Width = 150;
							RelationShipName.ReadOnly = false;
							dgStyleRelatedProducts.GridColumnStyles.Add(RelationShipName);
							// Add a second column style.
							DataGridColumnStyle RelationShipDescription = new DataGridTextBoxColumn();
							RelationShipDescription.MappingName = "RelationShipDescription";
							RelationShipDescription.HeaderText = "RelationShipDescription";
							RelationShipDescription.Width = 150;
							RelationShipDescription.ReadOnly = true;
							dgStyleRelatedProducts.GridColumnStyles.Add(RelationShipDescription);
							// Add a second column style.
							DataGridColumnStyle CategoryName = new DataGridTextBoxColumn();
							CategoryName.MappingName = "CategoryName";
							CategoryName.HeaderText = "CategoryName";
							CategoryName.Width = 150;
							CategoryName.ReadOnly = true;
							dgStyleRelatedProducts.GridColumnStyles.Add(CategoryName);
							dataGridRelationShip.TableStyles.Add(dgStyleRelatedProducts);
							// disallow new rows from being created
							dataGridRelationShip.DataSource = dt;
							CurrencyManager cmRelationship = (CurrencyManager)this.BindingContext[dataGridRelationShip.DataSource, dataGridRelationShip.DataMember];
							((DataView)cmRelationship.List).AllowNew = false;
							break;
							// treeViewParentCategoreis
							// treeViewChildCategories
					}
				}
			}
			// set the primary parent and current category
			txtParentCategory.Text = CategoryProperties.Tables["CategoryProperties"].Rows[0]["PrimaryParentCategory"].ToString();
			txtCurrentCategory.Text = CategoryProperties.Tables["CategoryProperties"].Rows[0]["CategoryName"].ToString();
			// now load parent categories
			LoadCategories(CSService.GetRootCategories(CategoryProperties.Tables["CategoryProperties"].Rows[0]["CatalogName"].ToString()));
		}

		private void LoadCategories(DataSet dsCategories)
		{
			foreach(DataRow dr in dsCategories.Tables[0].Rows)
			{
				// make sure to only add what we can
				CategoryNode  cnode = new CategoryNode(dr);
				treeViewParentCategoreis.Nodes.Add(cnode);
				treeViewParentCategoreis.SelectedImageIndex = 3;
				treeViewParentCategoreis.ImageIndex = 3;
			}
		}

		private void btnRemoveChildCategory_Click(object sender, System.EventArgs e)
		{
			// remove item
			listBoxAssignedChildCategories.Items.Remove(listBoxAssignedChildCategories.SelectedItem);
			// now if we have no items disable yourself
			if(listBoxAssignedChildCategories.Items.Count <= 0)
			{
				btnRemoveChildCategory.Enabled = false;
			}
		}

		private void btnAddChildCategory_Click(object sender, System.EventArgs e)
		{
			// add items only if it doesn't already exists
			if(!listBoxAssignedChildCategories.Items.Contains(listBoxAvailableChildCategories.SelectedItem))
			{
				listBoxAssignedChildCategories.Items.Add(listBoxAvailableChildCategories.SelectedItem);
			}
		}

		private void btnRemoveParentCategory_Click(object sender, System.EventArgs e)
		{
			// remove items
			listBoxAssiangedCategories.Items.Remove(listBoxAssiangedCategories.SelectedItem);
			// now if we have no items disable yourself
			if(listBoxAssiangedCategories.Items.Count <= 0)
			{
				btnRemoveParentCategory.Enabled = false;
				btnSet.Enabled = false;
			}
		}

		private void btnAddParentCategory_Click(object sender, System.EventArgs e)
		{
			string categoryName = treeViewParentCategoreis.SelectedNode.Text;
			// make sure we can't self reference
			if(categoryName != txtCurrentCategory.Text)
			{
				// move categories over only if it doesn't already exists
				if(!listBoxAssiangedCategories.Items.Contains(categoryName))
				{
					listBoxAssiangedCategories.Items.Add(categoryName);
				}
			}
		}

		private void listBoxAssignedChildCategories_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// enable the remove button if we have any values
			if(listBoxAssignedChildCategories.Items.Count > 0)
			{
				btnRemoveChildCategory.Enabled = true;
			}
		}

		private void listBoxAvailableChildCategories_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// enable the add button if we have any values
			if(listBoxAvailableChildCategories.Items.Count > 0)
			{
				btnAddChildCategory.Enabled = true;
			}
		}

		private void listBoxAssiangedCategories_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// enable the remove and set buttons only if we have values
			if(listBoxAssiangedCategories.Items.Count > 0)
			{
				btnRemoveParentCategory.Enabled = true;
				btnSet.Enabled = true;
			}
		}

		private void treeViewParentCategoreis_Click(object sender, System.EventArgs e)
		{
			// enable the add button only if we have categoreis
			if(treeViewParentCategoreis.Nodes.Count > 0)
			{
				btnAddParentCategory.Enabled = true;
			}
		}

		private void btnRelationShipCancel_Click(object sender, System.EventArgs e)
		{
			// undo
			((DataTable)dataGridRelationShip.DataSource).RejectChanges();
		}

		private void btnRelationShipRemove_Click(object sender, System.EventArgs e)
		{
			// remove current selection
			int currentRow = dataGridRelationShip.CurrentRowIndex;
			DataRow dr = ((DataTable)dataGridRelationShip.DataSource).Rows[currentRow];
			((DataTable)dataGridRelationShip.DataSource).Rows.Remove(dr);
		}

		private void btnRelationShipEdit_Click(object sender, System.EventArgs e)
		{
			int currentRow = dataGridRelationShip.CurrentRowIndex;
			if(currentRow!=-1)
			{
				// eidt current selection
				// show product picker
				ProductForms.ProductPicker ProdPicker = new ProductForms.ProductPicker();
				ProdPicker.ShowDialog();
				// update the row the user selected for the change
				DataRow objData = ProdPicker.txtSelection.Tag as DataRow;
				if(objData!=null)
				{
					DataRow RelationRow = ((DataTable)dataGridRelationShip.DataSource).Rows[currentRow];
					if(objData["ProductID"].ToString()!="")
					{
						RelationRow["TargetCatalog"]=objData["CatalogName"];
						RelationRow["ProductID"]=objData["ProductID"];
						RelationRow["RelationshipDescription"]="Product";
						RelationRow["CategoryName"]=null;
					}
					else
					{
						RelationRow["TargetCatalog"]=objData["CatalogName"];
						RelationRow["ProductID"]=null;
						RelationRow["RelationshipDescription"]="Category";
						RelationRow["CategoryName"]=objData["CategoryName"];
					}
				}
			}
		}

		private void btnRelationShipNew_Click(object sender, System.EventArgs e)
		{
			// show product picker
			ProductForms.ProductPicker ProdPicker = new ProductForms.ProductPicker();
			ProdPicker.ShowDialog();
			// now find out what the user selected
			if(!ProdPicker.isCancel)
			{
				// create the columns to work with
				DataTable dt = ((DataTable)dataGridRelationShip.DataSource);
				if(dt==null)
				{
					dt = new DataTable();
					// if we got a null value then create the columns
					dt.Columns.Add("TragetCatalog", typeof(string));
					dt.Columns.Add("ProductID", typeof(string));
					dt.Columns.Add("RelationShipName", typeof(string));
					dt.Columns.Add("RelationShipDescription", typeof(string));
					dt.Columns.Add("CategoryName", typeof(string));
				}
				// now check to see what we got a category or product
				DataRow objData = ProdPicker.txtSelection.Tag as DataRow;
				if(objData!=null)
				{
					// we got a product so insert the data into the datagrid
					GetRelationRow(dt, objData);
				}
			}
		}

		private void GetRelationRow(DataTable dt, DataRow dr)
		{
			// check to see if we havce a category column
			if(dt.Columns.Count==4)
			{
				dt.Columns.Add("CategoryName", typeof(string));
			}
			// if we got a null value then create the columns
			DataRow RelationRow = null;
			RelationRow = dt.NewRow();
			// make a relation data row
			// check to see if we are using a product
			if(dr["ProductID"].ToString()!="")
			{
				RelationRow["TargetCatalog"]=dr["CatalogName"];
				RelationRow["ProductID"]=dr["ProductID"];
				RelationRow["RelationshipName"]="";
				RelationRow["RelationshipDescription"]="Product";
				RelationRow["CategoryName"]=null;
				dt.Rows.Add(RelationRow);
			}
			else
			{
				RelationRow["TargetCatalog"]=dr["CatalogName"];
				RelationRow["ProductID"]=null;
				RelationRow["RelationshipName"]="";
				RelationRow["RelationshipDescription"]="Category";
				RelationRow["CategoryName"]=dr["CategoryName"];
				dt.Rows.Add(RelationRow);
			}
		}

		private void btnBack_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnSaveNBack_Click(object sender, System.EventArgs e)
		{
			// save our data and get out
			Save();
			this.Close();
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			// save our data
			Save();
		}

		private void Save()
		{
			// get category data
			GetCategoryData();
			// get parent categories
			GetParentCategories();
			// get child categories
			GetChildCategories();
			// get primary parent
			CategoryProperties.Tables["CategoryProperties"].Rows[0]["PrimaryParentCategory"]=txtParentCategory.Text;
			// now save the data
			CSService.SaveCategoryProperties(CategoryProperties, CategoryProperties.Tables["CategoryProperties"].Rows[0]["CatalogName"].ToString());			
		}

		private void GetParentCategories()
		{
			// for right now just remove everything then add
			CategoryProperties.Tables["ParentCategories"].Rows.Clear();

			// add the current parent categories
			for(int i=0; listBoxAssiangedCategories.Items.Count>i; i++)
			{
				// check to see if we need to add a row
				if(CategoryProperties.Tables["ParentCategories"].Rows.Count<listBoxAssiangedCategories.Items.Count)
				{
					DataRow dr = CategoryProperties.Tables["ParentCategories"].NewRow();
					CategoryProperties.Tables["ParentCategories"].Rows.Add(dr);
				}
				CategoryProperties.Tables["ParentCategories"].Rows[i]["Name"]=listBoxAssiangedCategories.Items[i].ToString();
			}
		}

		private void GetChildCategories()
		{
			// add the current child categories
			for(int i=0; listBoxAssignedChildCategories.Items.Count>i; i++)
			{
				// check to see if we need to add a row
				if(CategoryProperties.Tables["ChildCategories"].Rows.Count<listBoxAssignedChildCategories.Items.Count)
				{
					DataRow dr = CategoryProperties.Tables["ChildCategories"].NewRow();
					CategoryProperties.Tables["ChildCategories"].Rows.Add(dr);
				}
				CategoryProperties.Tables["ChildCategories"].Rows[i]["CategoryName"]=listBoxAssignedChildCategories.Items[i].ToString();
			}
		}

		private void GetCategoryData()
		{
			// go through the data grid and match product data
			foreach(DataRow dr in ((DataTable)dataGrid1.DataSource).Rows)
			{
				if(dr["DisplayName"].ToString()=="Price")
				{
					// check to make sure we have a value
					if(dr["Value"].ToString()!="")
					{
						CategoryProperties.Tables["CategoryProperties"].Rows[0]["cy_list_price"]=(object)dr["Value"];
					}
				}
				else
				{
					CategoryProperties.Tables["CategoryProperties"].Rows[0][dr["DisplayName"].ToString()]=(object)dr["Value"];
				}
			}
		}

		private void btnClear_Click(object sender, System.EventArgs e)
		{
			txtParentCategory.Text = "";
			btnClear.Enabled = false;
		}

		private void btnSet_Click(object sender, System.EventArgs e)
		{
			txtParentCategory.Text = listBoxAssiangedCategories.Text;
			btnClear.Enabled = true;
		}

		private void treeViewParentCategoreis_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			bool IsParent = false;
			// check to make sure we don't get anything that we have already have
			for(int i=0; listBoxAssiangedCategories.Items.Count>i; i++)
			{
				if(listBoxAssiangedCategories.Items[i].ToString()==e.Node.Text)
				{
					IsParent= true;
				}
			}
			
			if(!IsParent)
			{
				CategoryNode  catnode = (CategoryNode)e.Node;
				LoadCategories(catnode, CSService.ChildCategories(catnode.CategoryProperty["CatalogName"].ToString(), catnode.Text));
			}
		}

		private void LoadCategories(CategoryNode categoryNode, DataSet dsCategories)
		{
			try
			{
				foreach(DataRow dr in dsCategories.Tables[0].Rows)
				{
					// add the data row to each node so we don't have to 
					// go to the server again
					CategoryNode  cnode = new CategoryNode(dr);
					categoryNode.Nodes.Add(cnode);
					categoryNode.SelectedImageIndex = 3;
					categoryNode.ImageIndex = 3;
				}
			}
			catch{}
		}
	}
}
